<?php
/**
 * Created by PhpStorm.
 * User: niqiaoyun
 * Date: 2017/9/7
 * Time: 21:17
 */

namespace app\controllers;


use app\common\Repo;
use app\common\widgets\Tools;
use app\models\ProjectModel;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;

class ApplicationController extends BaseController {

	public function actionGetBranch() {
		$ary_page_data = [];
		$this->layout = false;
		$int_id = \Yii::$app->request->get("id");
		$project_config = ProjectModel::getConfig($int_id);
		//获取branch分支列表
		$version = Repo::getRevision($project_config);
//		$list = $version->getBranchList();

		//mock数据
		$list = $this->getMockData();
		$ary_page_data['lists'] = $list;
		return $this->render('get-branch', $ary_page_data);
	}

	public function getMockData() {
		$ary_data = [
			[
				"id" => "develop",
				"name" => "develop"
			],
			[
				"id" => "master",
				"name" => "master"
			],
		];
		return $ary_data;
	}

	/**
	 * 获取commit历史
	 * @param $id
	 * @param string $branch
	 * @return string
	 * @throws \Exception
	 */
	public function actionGetCommitHistory($id, $branch = 'master') {
		$this->layout = false;
		$config = ProjectModel::getConfig($id);
		$revision = Repo::getRevision($config);
		if ($config->repo_mode == ProjectModel::REPO_MODE_TAG && $config->repo_type == ProjectModel::REPO_TYPE_GIT) {
//			$list = $revision->getTagList();
			$list = $this->getMockCommitHistory();
		} else {
//			$list = $revision->getCommitList($branch);
			$list = $this->getMockCommitHistory($branch);
		}
//		echo "<pre>";print_r($list);exit;
		$ary_page_data['lists'] = $list;
		return $this->render("get-commit-history", $ary_page_data);
	}

	public function getMockCommitHistory($branch) {
		if("master" == $branch) {
			$str_json = '{"code":0,"msg":"","data":[{"id":"7d1f500","name":"7d1f500 - hui.wang merge"},{"id":"41438c4","name":"41438c4 - 神童小子 Initial commit"}]}';
		} else {
			$str_json = '{"code":0,"msg":"","data":[{"id":"a521c3a","name":"a521c3a - hui.wang 项目架构"},{"id":"7049ccc","name":"7049ccc - hui.wang 项目架构"},{"id":"3a0534a","name":"3a0534a - hui.wang 项目架构"},{"id":"627c967","name":"627c967 - hui.wang 项目架构"},{"id":"6ecf034","name":"6ecf034 - hui.wang 项目架构"},{"id":"31ea219","name":"31ea219 - hui.wang 项目架构"},{"id":"38b6d4e","name":"38b6d4e - hui.wang 项目架构"},{"id":"d5b75fd","name":"d5b75fd - hui.wang 项目架构c111"},{"id":"210f87f","name":"210f87f - hui.wang 项目架构"},{"id":"88c5635","name":"88c5635 - hui.wang 项目架构"},{"id":"c3cb2d1","name":"c3cb2d1 - hui.wang 项目架构"},{"id":"678b527","name":"678b527 - hui.wang 项目架构"},{"id":"a4ec776","name":"a4ec776 - hui.wang 项目架构"},{"id":"04b06b7","name":"04b06b7 - hui.wang 项目架构"},{"id":"24c5386","name":"24c5386 - hui.wang 项目架构"},{"id":"7764bf3","name":"7764bf3 - hui.wang 项目架构"},{"id":"4c473c6","name":"4c473c6 - hui.wang 项目架构"},{"id":"152ea9f","name":"152ea9f - hui.wang 项目架构"},{"id":"be91be3","name":"be91be3 - hui.wang model"},{"id":"b5473dd","name":"b5473dd - hui.wang update"}]}';
		}

		$ary_list = Json::decode($str_json, true);
		return $ary_list['data'];
	}
}